
const rootPath = '../../../';
const router = require('express').Router();
const baseTool = require(rootPath + 'common/base-tool');
const tips = require(rootPath + 'common/tips');
const log = require(rootPath + 'common/log');
const CURD = require(rootPath + 'compontents/DB/CURDClass')
const RSA = require(rootPath + 'common/RSAClass');

router.post('/create', async (req, res, next) => {
    const r = req.body;

    let xhtk = req.cookies.xhtk;
    const uid = req.cookies.uid;
    if (xhtk && uid) {
        xhtk = await new RSA().decrypt(xhtk);
        const token = xhtk.split('_XHPX_')[1];
        const login_rows = await CURD.select({
            fields: 'id',
            table: 'user',
            where: [
                {wfield: 'uid', wvalue: uid},
                {wfield: 'token', wvalue: token}
            ]
        });
        if (login_rows.length) {
                /**
                 * =======================
                 *   更新角色性格
                 * =======================
                 */
                const user_info_rows = await CURD.select({
                    fields: '`id`',
                    table: 'user_info',
                    where: [
                        {wfield: 'uid', wvalue: uid}
                    ]
                });
                if (user_info_rows.length) {
                    let filed ='';
                    if (r.character) {
                        // 确认天赋
                        filed = [`\`character\`=${r.character}`]
                    } else {
                        // 选择天赋
                        filed = [`\`character\`=0`]
                    }
                    filed.push(`\`birth\`=${r.step}`)
                    const change_user_info = await CURD.update({
                        table:'user_info',
                        fields: filed,
                        where: [
                            {
                                wfield: 'id',
                                wvalue: user_info_rows[0].id
                            }
                    ]})
                    if (change_user_info.changedRows) {
                        res.json({
                            code: tips.successCode,
                            msg: tips.success,
                            status: r.step
                        })
                    }
                } else {
                    /**
                     * =======================
                     *   创建角色基础信息
                     * =======================
                     */
                    // 验证
                    if (!r.str) {
                        res.json({
                            code: tips.gift.failCode,
                            msg: '请输入臂力值'
                        });
                        return;
                    }
                    if (!r.int) {
                        res.json({
                            code: tips.gift.failCode,
                            msg: '请输入悟性值'
                        });
                        return;
                    }
                    if (!r.con) {
                        res.json({
                            code: tips.gift.failCode,
                            msg: '请输入根骨值'
                        });
                        return;
                    }
                    if (!r.dex) {
                        res.json({
                            code: tips.gift.failCode,
                            msg: '请输入身法值'
                        });
                        return;
                    }
                    if (parseInt(r.str)+parseInt(r.int)+parseInt(r.con)+parseInt(r.dex)!=80) {
                        res.json({
                            code: tips.gift.failCode,
                            msg: tips.gift.fail
                        });
                        return;
                    }
                    try {
                        const ctime = Math.round(new Date().getTime() / 1000);
                        const user_info_rows = await CURD.insert({
                            table: `user_info`,
                            fields: 'uid, cname, con,con_last, dex,dex_last, int,int_last, str,str_last, gender,live_time,character',
                            values: `${uid}, ${r.cname}, ${r.con},${r.con}, ${r.dex},${r.dex}, ${r.int},${r.int}, ${r.str},${r.str}, ${r.gender}, ${ctime},0`
                        });
                        if (user_info_rows.affectedRows) {
                            res.json({
                                code: tips.successCode,
                                msg: tips.success,
                                status: 0
                            })
                        }
                    } catch (err) {
                        log.err(err)
                    }
                }
        } else {
            // 本地cookie被改
            res.cookie('xhtk', 0, {httpOnly: true, maxAge: 0});
            res.cookie('uid', 0, {httpOnly: true, maxAge: 0});
            res.json({
                code: tips.loginTimeOver.failCode,
                msg: tips.loginTimeOver.fail
            });
        }
    } else {
        res.cookie('xhtk', xhtk, {httpOnly: true, maxAge: 0});
        res.cookie('uid', uid, {httpOnly: true, maxAge: 0});
        res.json({
            code: tips.loginTimeOver.failCode,
            msg: tips.loginTimeOver.fail
        });
    }
});
module.exports = router;